import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Query,
  UsePipes,
} from '@nestjs/common';
import { ManagerServiceImpl } from '@/api/admin/service/impl/managerServiceImpl';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import {
  ManagerInsetDto,
  ManagerDelteDto,
  ManagerFindDto,
  ManagerListDto,
  ManagerUpdateDto,
  ManagerSaveAtferExistDto,
} from '@/api/admin/dto/manager.dto';
import { ManagerEntity } from '@/entity/manager.entity';
import { pageRecords } from '@/interface/pageInterface';

@ApiBearerAuth()
@ApiTags('后台用户管理')
@Controller('admin/manager')
export class ManagerController {
  constructor(private readonly ManagerServiceImpl: ManagerServiceImpl) {}

  @ApiOperation({ description: '创建管理员' })
  @Post('/insert')
  // TODO DTO的问题。
  async saveUser(@Body() body: ManagerSaveAtferExistDto): Promise<unknown> {
    const { field, value, manager } = body;
    return await this.ManagerServiceImpl.saveAtferExist(field, value, manager);
  }
  @ApiOperation({ description: '删除管理员' })
  @Post('/delete')
  async delete(@Body() body: ManagerDelteDto): Promise<unknown> {
    const { id } = body;
    return await this.ManagerServiceImpl.deleteOneById(id);
  }
  @ApiOperation({ description: '根据ID查找一个管理员' })
  @Get('/find/:id')
  async find(@Param() params: ManagerFindDto) {
    const { id } = params;
    return await this.ManagerServiceImpl.getOneById(id);
  }

  @ApiOperation({ description: '查找所有管理员' })
  @Get('/list')
  async list(
    @Query() query: ManagerListDto,
  ): Promise<pageRecords<ManagerEntity>> {
    // console.log('controller', body);
    const { pageSize, pageNumber, field, sort } = query;
    return await this.ManagerServiceImpl.getAllWithPageOrOrder(
      field,
      sort,
      pageSize,
      pageNumber,
    );
  }

  /**
   *
   * @param body
   */
  @ApiOperation({ description: '更新管理员' })
  @Post('/update')
  // DTO也是
  async update(@Body() body: ManagerUpdateDto) {
    const { id, newData } = body;
    return await this.ManagerServiceImpl.update(id, newData);
  }
}
